/*
 * @lc app=leetcode.cn id=350 lang=cpp
 *
 * [350] 两个数组的交集 II
 */

// @lc code=start
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_multiset<int> memo;
        for(auto x : nums1) memo.insert(x);

        vector<int> ans;
        for(auto x : nums2){
            if(memo.count(x)){
                ans.push_back(x);
                memo.erase(memo.find(x));
            }
        }

        return ans;
    }
};
// @lc code=end

